package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserInfoVo;
import com.wf.captcha.SpecCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;


@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    SystemUserMapper systemUserMapper;

    public CaptchaVo getCaptcha() {
        //把验证码图片创建出来
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        //获取到验证码图片中的验证码,并且变为小写
        String lowerCase = specCaptcha.text().toLowerCase();
        //创建key
        String key = "admin:login:" + UUID.randomUUID();
        //写到数据库,并设置key-value的过期时间为60s
        redisTemplate.opsForValue().set(key, lowerCase, 60, TimeUnit.SECONDS);
        //把图片进行toBase64编码
        CaptchaVo captchaVo = new CaptchaVo(specCaptcha.toBase64(), key);
        return captchaVo;
    }

    public String login(LoginVo loginVo) {
        //验证码为空
        if (loginVo.getCaptchaCode() == null) {
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
        }
        //验证码过期
        if (redisTemplate.opsForValue().get(loginVo.getCaptchaKey()) == null) {
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
        //验证码错误
        if (!loginVo.getCaptchaCode().equals(redisTemplate.opsForValue().get(loginVo.getCaptchaKey()))) {
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }

        //账号不存在
        SystemUser systemUser = systemUserMapper.selectOneByUserName(loginVo.getUsername());
        if (systemUser == null) {
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        //账号被锁定
        if (systemUser.getStatus() == BaseStatus.DISABLE) {
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }
        //用户名或密码错误
        if (!systemUser.getPassword().equals(DigestUtils.md5DigestAsHex(loginVo.getPassword().getBytes()))) {
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        //生成jwt令牌
        String jwt = JwtUtil.createJWT(systemUser.getId(), systemUser.getUsername());
        return jwt;

    }



    public SystemUserInfoVo getUserInfoByUserId(Long userId) {
        SystemUser systemUser = systemUserMapper.selectById(userId);
        SystemUserInfoVo systemUserInfoVo = new SystemUserInfoVo();
        systemUserInfoVo.setName(systemUser.getName());
        systemUserInfoVo.setAvatarUrl(systemUser.getAvatarUrl());
        return systemUserInfoVo;
    }
}













